require(['./config'], function() {
	require(['jquery', 'template', 'loadHF'], function($, template) {
		class order {
			constructor() {
				this.cart = JSON.parse(localStorage.getItem('cart')) || []
				this.ordercart = []
				this.showcart()
				this.addListener()

			}
			showcart() {
				if (this.cart == 0) {
					$(".nothing").show()
					$(".gwcorderlist").hide()
					// console.log($(".gwcorderlist"))
				} else {
					$(".nothing").hide()
					$(".gwcorderlist").show()
					// console.log(this.cart)
					const list = {
						cart: this.cart
					}
					const html = template("initlist", list)
					$(".cart-body").html(html)
				}
			}
			addListener() {
				// 增
				$(".cart-body").on("click", ".increment", this.addamount.bind(this))
				// 减
				$(".cart-body").on("click", ".decrement", this.subamount.bind(this))
				// // 删
				$(".cart-body").on("click", ".del", this.delproduct.bind(this))
				// // 改
				$(".cart-body").on("blur", ".prod-amount", this.changeamount.bind(this))
				// // 全选
				$(".chk_all").on("click", this.allcheck.bind(this))
				// 单选
				$(".cart-body").on("click", ".thecheckbox", this.oncheck.bind(this))
				$(".goorder").on("click", this.suborder.bind(this))
			}
			addamount(e) {
				// console.log("aa")
				const parent = $(e.target).parents("tr")
				const _id = parent.data("id")
				const prod = this.cart.find(function(aa) {
					return aa.id === _id
				})
				prod.amount += 1
				localStorage.cart = JSON.stringify(this.cart)
				parent.find(".prod-amount").val(prod.amount)
				parent.find(".sub").text("￥" + (prod.price * prod.amount).toFixed(2))
				this.monamount()
				if (this.cart == 0) {
					$(".cartamount").text(0)
				} else {
					let s = 0
					this.cart.forEach(function(a) {
						s += a.amount
					})
					$(".cartamount").text(s)
				}
			}
			subamount(e) {
				// console.log("aa")
				const parent = $(e.target).parents("tr")
				const _id = parent.data("id")
				const prod = this.cart.find(function(aa) {
					return aa.id === _id
				})
				prod.amount -= 1
				if (prod.amount <= 1) {
					prod.amount = 1
				}
				localStorage.cart = JSON.stringify(this.cart)
				parent.find(".prod-amount").val(prod.amount)
				parent.find(".sub").text("￥" + (prod.price * prod.amount).toFixed(2))
				this.monamount()
				if (this.cart == 0) {
					$(".cartamount").text(0)
				} else {
					let s = 0
					this.cart.forEach(function(a) {
						s += a.amount
					})
					$(".cartamount").text(s)
				}
			}
			changeamount(e) {
				// console.log("aa")
				const parent = $(e.target).parents("tr")
				const _id = parent.data("id")
				const prod = this.cart.find(function(aa) {
					return aa.id === _id
				})
				prod.amount = Number($(e.target).val())
				localStorage.cart = JSON.stringify(this.cart)
				parent.find(".prod-amount").val(prod.amount)
				parent.find(".sub").text("￥" + (prod.price * prod.amount).toFixed(2))
				this.monamount()
				if (this.cart == 0) {
					$(".cartamount").text(0)
				} else {
					let s = 0
					this.cart.forEach(function(a) {
						s += a.amount
					})
					$(".cartamount").text(s)
				}
			}
			delproduct(e) {
				const parent = $(e.target).parents("tr")
				const _id = parent.data("id")
				this.cart.forEach((a, b) => {
					if (a.id == _id) {
						this.cart.splice(b, 1)
					}
				})
				// console.log(this.cart)
				localStorage.cart = JSON.stringify(this.cart)
				parent.remove()
				if (this.cart == 0) {
					$(".nothing").show()
					$(".gwcorderlist").hide()
					// console.log($(".gwcorderlist"))
				}
				this.monamount()
				if (this.cart == 0) {
					$(".cartamount").text(0)
				} else {
					let s = 0
					this.cart.forEach(function(a) {
						s += a.amount
					})
					$(".cartamount").text(s)
				}
			}
			allcheck(e) {
				const tf = $(e.target).prop("checked")
				console.log(tf)
				$(".thecheckbox").prop("checked", tf)
				this.monamount()
			}
			oncheck() {
				if ($(".thecheckbox:checked").length === this.cart.length) {
					$(".chk_all").prop("checked", true)
				} else {
					$(".chk_all").prop("checked", false)
				}
				this.monamount()
			}
			monamount() {
				let s = 0
				const aa = $(".thecheckbox:checked")
				// console.log($(".thecheckbox:checked"))
				const len = $(".thecheckbox:checked").length
				for (let i = 0; i < len; i++) {
					// console.log($(aa[i]).parents("tr"))
					const _tr = $(aa[i]).parents("tr")
					// console.log($(_tr).find(".sub").text().slice(1))
					s += Number($(_tr).find(".sub").text().slice(1))
				}
				// this.cart.forEach(function(a){
				// 	s+=a.amount*a.price
				// 	// console.log(s)
				// })
				// console.log(s)
				$(".total-price").text("￥" + s.toFixed(2))
			}
			suborder() {
				// console.log("aaa")
				const aa = $(".thecheckbox:checked")
				const len = $(".thecheckbox:checked").length
				for (let i = 0; i < len; i++) {
					const _tr = $(aa[i]).parents("tr")
					// console.log(_tr.find(".prod-amount").val())
					const orderlist = {
						id: _tr.data("id"),
						img: _tr.find("img").attr("src"),
						title: _tr.find("td:nth-of-type(4)").text(),
						price: _tr.find("td:nth-of-type(5)").text().slice(1),
						amount: _tr.find(".prod-amount").val()
					}
					this.ordercart.push(orderlist)
				}
				console.log(this.ordercart)
				// let storage=window.localStorage
				// storage.setItem("ordercart",JSON.stringify(this.ordercart));
				localStorage.setItem("ordercart", JSON.stringify(this.ordercart))
				if (this.ordercart == "") {
					// console.log("sdfsd")
					$(".zgc").css("display", "block")
					$(".wxg").slideDown("slow");
					$(".qd").on("click", function() {
						$(".zgc").css("display", "none")
						$(".wxg").slideUp("slow");
					})
				} else {
					window.location = "/html/order.html"
				}
				this.ordercart = []
			}
		}
		new order()
	})
})
